<?php

/**
 * 用户公众号搜索牌号数据
 * @author yu
 */
namespace quote\service;

use common\service\CommonService;
use herosphp\core\Loader;

class UserSearchBrandRecordService extends CommonService
{
    public function __construct()
    {
        parent::__construct('quoteData');
    }

    /**
     * 添加获取记录
     * @param $saas_user_id
     * @param $brand_name
     * @return bool
     */
    public function addRecord($saas_user_id, $brand_name)
    {

        $userSearchBrandRecordModel = Loader::model('UserSearchBrandRecord');
        $db = $userSearchBrandRecordModel->getDB();

        $item = $userSearchBrandRecordModel->getItem(array('saas_user_id' => $saas_user_id, 'brand_name' => $brand_name));

        if ($item) {

            $updateSql = "Update user_search_brand_record Set get_times=get_times+1 ,updatetime=now() Where id='{$item['id']}'";

            if ($db->excute($updateSql) === false) {

                return false;
            }

        } else {

            $data = array(
                'saas_user_id' => $saas_user_id,
                'brand_name' => $brand_name,
                'get_times' => 1,
                'addtime' => myNow(),
                'updatetime' => myNow(),
            );

            if (!$userSearchBrandRecordModel->add($data)) {

                return false;
            }
        }

        return true;

    }

}